package screens
{
	import elements.MaParticule;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import flash.geom.Point;
	import starling.display.Quad;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import elements.MonSprite;
	import starling.textures.Texture;
	import starling.display.Image;
	import starling.events.TouchPhase;
	import flash.display.Bitmap;
	import flash.media.*;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	import flash.utils.clearTimeout;
	import events.Nav;
	import actionscript.*;
	//import flash.events.*;

	import MultiTouch.Events.MTouchEvent;
	
	public class InApi extends Screen
	{
		
		public var toucher:Touch;
		public var q:Quad;
		
		//tableau de particules
		public var tabParticulesA:Array;
		
		public var _par:MaParticule;
		
		
		//timer pour retourner à home/
		private var _timer:Timer; 
		private var sonAmbiance:Sound;
		private var sdChannel:SoundChannel;
		
		//compteur
		private var cpt:int;
		

		//constructor
		public function InApi():void
		{
			super();
			tabParticulesA = new Array(new MaParticule(), new MaParticule(), new MaParticule(),new MaParticule());			
			
		}
		
		override public function drawContent():void
		{
			q = new Quad(1280, 800, 0x000000, false);
			q.alpha = 0;
			this.addChild(q);

			//mettre en place le fond sonore
			this.setFondSonore();
			
			//on met le compteur à ZERO
			cpt = 0;

			this.addEventListener(TouchEvent.TOUCH, testTouch);
			this.addEventListener(MTouchEvent.MOUSE_DOWN, MtouchHandler);
		}
		
		protected function MtouchHandler(evt:MTouchEvent):void 
		{
		
		}
		
		public function gestionDessin(x:int, y:int):void
		{
			//recuperer les deux coordonnée pour afficher un dessin lors du toucher
		}
					
		public function testTouch(evt: starling.events.TouchEvent):void
		{
			if (_timer!=null)
			{
				_timer.stop();
			}
			var touche:Touch = evt.getTouch(stage);

			if (touche && touche.phase == TouchPhase.BEGAN)
            {
				if (cpt >= tabParticulesA.length)
				{
					cpt = 0;
					var pt:Point = touche.getLocation(stage);
					//_par.ps.emitterX = touche.globalX;
					
					//_par.ps.emitterY = touche.globalY;
				trace("test particule");
					//on anjoute un nouvel élément à la scene
					addChild(tabParticulesA[cpt]);
					tabParticulesA[cpt]._hitbox.x = pt.x;
					tabParticulesA[cpt]._hitbox.y = pt.y;
					
					tabParticulesA[cpt].ps.emitterX = pt.x;
					tabParticulesA[cpt].ps.emitterY = pt.y;
					
					cpt++;
				}
				else
				{
					var pt2:Point = touche.getLocation(stage);
					//_par.ps.emitterX = touche.globalX;
					
					//_par.ps.emitterY = touche.globalY;
					//on anjoute un nouvel élément à la scene
					addChild(tabParticulesA[cpt]);
					tabParticulesA[cpt]._hitbox.x = pt2.x;
					tabParticulesA[cpt]._hitbox.y = pt2.y;
					
					tabParticulesA[cpt].ps.emitterX = pt2.x;
					tabParticulesA[cpt].ps.emitterY = pt2.y;
					
					cpt++;
				}
				
				
				
            }
			timerFonction();
		}
		
		public function setFondSonore():void
		{
			//la fonction va chercher le mp3 pour le mettre en fond sonore
			trace("- Fond sonore -");
			sdChannel = new SoundChannel();
			sonAmbiance = new Sound();
			sonAmbiance.load(new URLRequest("../src/Assets/sons/ambiance.mp3"));
			sonAmbiance.addEventListener(flash.events.Event.COMPLETE,onComplete,false,0,true)
		}
		
		public function timerFonction(): void
		{
			_timer = new Timer(100000, 1);
			_timer.addEventListener(TimerEvent.TIMER, changeScreen);
			_timer.start();
		}
	
		public function changeScreen(event:TimerEvent): void
		{
			this.dispatchEvent(new Nav(Nav.CHANGE_SCREEN, { id:"home" }, true));
			
		}
		public function onComplete(evt:Event):void
		{
			//sndChannel = sonAmbiance.play(0, 99);
		}
		public function songControl(bo:Boolean):void
		{
			if (bo==true)
			{
				
				sdChannel = sonAmbiance.play(0, 99);
			}
			else 
			{
				
				sdChannel.stop();
			}
		}
		
	}
}